﻿Imports Abyx.SVCMS.Library
Imports Abyx.SVCMS.Library.DB
Imports Abyx.WindowsExtendedControls

Public Class STK_BulkEntry_Form
    Implements ICarfDataEditingChildWindow


#Region "Declarations"
    Dim iDataObject As DataTable = Nothing
    Dim iDataCategory As DataTable = Nothing
    Dim iDataSubCategory As DataTable = Nothing
    Dim iDataModel As DataTable = Nothing
    Dim iDataBrand As DataTable = Nothing
    Dim iDataColor As DataTable = Nothing
    Dim iDataWeight As DataTable = Nothing
    Dim iDataVolume As DataTable = Nothing
    Dim iDataManufacturer As DataTable = Nothing
    Dim iDataUnit As DataTable = Nothing

    Dim Category_Column As DatagridViewEdComboBoxColumn
    Dim SubCategory_Column As DatagridViewEdComboBoxColumn
    Dim Model_Column As DatagridViewEdComboBoxColumn
    Dim Brand_Column As DatagridViewEdComboBoxColumn
    Dim Color_Column As DatagridViewEdComboBoxColumn
    Dim Weight_Column As DatagridViewEdComboBoxColumn
    Dim Volume_Column As DatagridViewEdComboBoxColumn
    Dim Manufacturer_Column As DatagridViewEdComboBoxColumn
    Dim Unit_Column As DatagridViewEdComboBoxColumn

    Dim MenuCheckStateFromGridHasBeenInitialized As Boolean = False

    Dim frmMain As SVCM_Form = Nothing

#End Region

#Region "Constructors"
    Public Overloads Sub Show()
        MyBase.Show()
        Me.frmMain = Me.MdiParent
        Setup_StockGrid()
        UpdateCheckState_FromGridToMenu()
        Refresh_ControlsWithDataObject()
    End Sub

#End Region

#Region "Events"
    Private Sub STK_StockBulkEntry_Form_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.InitializeThisFormAndControlSettings()
    End Sub
    Private Sub STK_StockBulkEntry_Form_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.InitializeThisFormAndControlSettings(True)
    End Sub

    'SparesStock_DataGridView
    Private Sub SparesStock_DataGridView_CellMouseEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles SparesStock_DataGridView.CellMouseEnter
        If e.RowIndex <> -1 AndAlso e.ColumnIndex <> -1 Then
            Dim cell As DataGridViewCell = SparesStock_DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)
            If cell.ToolTipText <> "" Then
                Me.ToolTip1.SetToolTip(SparesStock_DataGridView, cell.ToolTipText)
            Else
                Me.ToolTip1.Hide(SparesStock_DataGridView)
            End If
        Else
            Me.ToolTip1.Hide(SparesStock_DataGridView)
        End If
    End Sub
    Private Sub SparesStock_DataGridView_DataError(sender As Object, e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles SparesStock_DataGridView.DataError

        'AMsg.ShowDialog(e.Exception.Message, MsgBoxStyle.Critical, e.Exception)
        'Dim mainForm As SVCM_Form = Me.MdiParent
        'mainForm.StatusInfoMsgPut("Data error row: " & e.RowIndex & " column: " & e.ColumnIndex & " message: " & e.Exception.Message)

    End Sub
    Private Sub SparesStock_DataGridView_CellValidating(sender As Object, e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles SparesStock_DataGridView.CellValidating
        If TypeOf SparesStock_DataGridView.Columns(e.ColumnIndex) Is DataGridViewComboBoxColumn Then
            Dim comboBoxColumn As DataGridViewComboBoxColumn = SparesStock_DataGridView.Columns(e.ColumnIndex)
            If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
                If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
                    Dim i As Integer = comboBoxColumn.Items.Add(e.FormattedValue)
                    SparesStock_DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = e.FormattedValue
                End If
            End If
        End If
    End Sub
    Private Sub SparesStock_DataGridView_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles SparesStock_DataGridView.CellEndEdit
        If (e.RowIndex <> -1) AndAlso (e.ColumnIndex <> -1) Then
            Dim cell As DataGridViewCell = SparesStock_DataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex)
            If IsDBNull(cell.Value) = False Then
                Select Case Me.SparesStock_DataGridView.Columns(e.ColumnIndex).Name
                    Case Is = Category_Column.Name
                        If DoesCellValueExistInHistoryData(cell.Value, Me.iDataCategory) = False Then
                            Me.iDataCategory.Rows.Add(cell.Value)
                            Refresh_CategoriesList()
                        End If
                    Case Is = SubCategory_Column.Name
                        If DoesCellValueExistInHistoryData(cell.Value, Me.iDataSubCategory) = False Then
                            Me.iDataSubCategory.Rows.Add(cell.Value)
                            Refresh_SubCategoryList()
                        End If
                    Case Is = Unit_Column.Name
                        If DoesCellValueExistInHistoryData(cell.Value, Me.iDataUnit) = False Then
                            Me.iDataUnit.Rows.Add(cell.Value)
                            Refresh_UnitList()
                        End If
                    Case Is = Model_Column.Name
                        If DoesCellValueExistInHistoryData(cell.Value, Me.iDataModel) = False Then
                            Me.iDataModel.Rows.Add(cell.Value)
                            Refresh_ModelList()
                        End If
                    Case Is = Brand_Column.Name
                        If DoesCellValueExistInHistoryData(cell.Value, Me.iDataBrand) = False Then
                            Me.iDataBrand.Rows.Add(cell.Value)
                            Refresh_BrandList()
                        End If
                    Case Is = Color_Column.Name
                        If DoesCellValueExistInHistoryData(cell.Value, Me.iDataColor) = False Then
                            Me.iDataColor.Rows.Add(cell.Value)
                            Refresh_ColorList()
                        End If
                    Case Is = Weight_Column.Name
                        If DoesCellValueExistInHistoryData(cell.Value, Me.iDataWeight) = False Then
                            Me.iDataWeight.Rows.Add(cell.Value)
                            Refresh_WeightList()
                        End If
                    Case Is = Volume_Column.Name
                        If DoesCellValueExistInHistoryData(cell.Value, Me.iDataVolume) = False Then
                            Me.iDataVolume.Rows.Add(cell.Value)
                            Refresh_VolumeList()
                        End If
                    Case Is = Manufacturer_Column.Name
                        If DoesCellValueExistInHistoryData(cell.Value, Me.iDataManufacturer) = False Then
                            Me.iDataManufacturer.Rows.Add(cell.Value)
                            Refresh_ManufacturerList()
                        End If
                End Select
            End If
        End If

    End Sub
    Private Sub SparesStock_DataGridView_DefaultValuesNeeded(sender As Object, e As System.Windows.Forms.DataGridViewRowEventArgs) Handles SparesStock_DataGridView.DefaultValuesNeeded
        'get new itemcode
        Dim stkMan As New StkManager(AppSettings.LoggedUser)
        Dim col As New Collections.ObjectModel.Collection(Of Integer)
        Dim dt As DataTable = Me.iDataObject.GetChanges()
        If dt IsNot Nothing Then
            For Each row As DataRow In dt.Rows
                If row.RowState <> DataRowState.Deleted Then
                    If (row.Item("ItemCode") IsNot Nothing) _
                        AndAlso (IsNumeric(row.Item("ItemCode"))) Then
                        col.Add(row.Item("ItemCode"))
                    End If
                End If
            Next
        End If
        Dim itemCode As String = stkMan.Get_StockItemCode_Formatted(col)
        With e.Row
            .Cells("StockItemId_Column").Value = Guid.NewGuid
            .Cells("ItemCode_Column").Value = itemCode
            .Cells("Quantity_Column").Value = 0
            .Cells("CostPrice_Column").Value = 0
            .Cells("SellingPrice_Column").Value = 0
            .Cells("TaxPercentage_Column").Value = 6
            '.Cells("HasSerial_Column").Value = "N"
            .Cells("CreatedDate_Column").Value = Now
            .Cells("ModifiedDate_Column").Value = Now
        End With
        SparesStock_DataGridView.Refresh()
    End Sub
    Private Sub SparesStock_DataGridView_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles SparesStock_DataGridView.EditingControlShowing

        If TypeOf e.Control Is DataGridViewComboBoxEditingControl Then
            Dim cmb As DataGridViewComboBoxEditingControl = e.Control
            cmb.DropDownStyle = ComboBoxStyle.DropDown
        End If

    End Sub
    Private Sub SparesStock_DataGridView_RowValidating(sender As Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles SparesStock_DataGridView.RowValidating
        Dim row As DataGridViewRow = Me.SparesStock_DataGridView.Rows(e.RowIndex)
        Me.ValidateAndHighlightDataGridRow(row)
    End Sub
    Private Sub SparesStock_DataGridView_DoubleClick(sender As Object, e As System.EventArgs) Handles SparesStock_DataGridView.DoubleClick
        If (Me.SparesStock_DataGridView.CurrentRow IsNot Nothing) Then
            If Me.SparesStock_DataGridView.CurrentRow.IsNewRow = False Then
                If (Me.SparesStock_DataGridView.CurrentRow.DataBoundItem IsNot Nothing) _
                    AndAlso (TypeOf Me.SparesStock_DataGridView.CurrentRow.DataBoundItem Is DataRowView) Then
                    Dim row As DataRow = CType(Me.SparesStock_DataGridView.CurrentRow.DataBoundItem, DataRowView).Row
                    If row.RowState = DataRowState.Unchanged Then
                        Dim stockItem As New StockItemType
                        stockItem.Load(row)
                        Dim stockItemInventory As New STK_ItemInventory
                        stockItemInventory.MdiParent = Me.MdiParent
                        stockItemInventory.Show(stockItem)
                    Else
                        Me.frmMain.StatusInfoMsgPut("Item has been changed, and changes has to be commited before updating inventory")
                    End If
                End If
            End If
        End If
    End Sub

    'Columns_ContextMenuItems
    Private Sub Description_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles Description_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub Category_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles Category_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub SubCategory_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles SubCategory_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub Model_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles Model_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub Brand_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles Brand_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub Color_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles Color_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub Weight_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles Weight_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub Volume_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles Volume_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub Manufacturer_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles Manufacturer_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub Unit_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles Unit_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub Quantity_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles Quantity_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub CostPrice_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles CostPrice_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub SellingPrice_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles SellingPrice_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub TaxPercentage_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles TaxPercentage_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub CreatedDate_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles CreatedDate_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub
    Private Sub ModifiedDate_ToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles ModifiedDate_ToolStripMenuItem.CheckedChanged
        UpdateCheckState_FromMenuToGrid()
    End Sub

    'Search_ImageButton
    Private Sub Search_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Search_ImageButton.Click
        Refresh_SparesDataSet()
        Refresh_StockDataGrid()
    End Sub
    Private Sub Search_TextBox_TextChanged(sender As Object, e As System.EventArgs) Handles Search_TextBox.TextChanged
        Search_ImageButton.PerformClick()
    End Sub

    'Commit_ImageButton
    Private Sub Commit_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Commit_ImageButton.Click
        Dim dt As DataTable = Me.iDataObject.GetChanges
        If (dt IsNot Nothing) AndAlso (dt.Rows.Count > 0) Then
            Dim stkman As New StkManager(AppSettings.LoggedUser)
            Dim res As DataTable = stkman.Update_StockItem_Batch(dt)
            Dim doesErrorExist As Boolean = False
            If (res IsNot Nothing) AndAlso (res.Rows.Count > 0) Then
                For Each row As DataRow In res.Rows
                    If row.RowState = DataRowState.Deleted Then
                        If (IsDBNull(row.Item("UpdateStatus", DataRowVersion.Original)) = False) _
                                                AndAlso (row.Item("UpdateStatus", DataRowVersion.Original) <> "") Then
                            doesErrorExist = True
                            Exit For
                        End If
                    Else
                        If (IsDBNull(row.Item("UpdateStatus")) = False) _
                        AndAlso (row.Item("UpdateStatus") <> "") Then
                            doesErrorExist = True
                            Exit For
                        End If
                    End If
                Next
            End If
            If doesErrorExist Then
                Dim dlg As New STK_BulkEntryDialog_Form
                dlg.iDataObject = res
                dlg.ShowDialog()
                Me.Refresh_ControlsWithDataObject()
            Else
                Dim main As SVCM_Form = Me.MdiParent
                main.StatusInfoMsgPut("Requested changes have been successfully updated to database")
                Me.Refresh_ControlsWithDataObject()
            End If
        Else
            Dim main As SVCM_Form = Me.MdiParent
            main.StatusInfoMsgPut("No changes were detected to commit")
        End If
    End Sub

    'Cancel_ImageButton
    Private Sub Cancel_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Cancel_ImageButton.Click
        Me.Close()
    End Sub

#End Region

#Region "ICARF Implementations"
    Public Function GetFormInstanceForDataObject(DataObject As Object) As System.Windows.Forms.Form Implements ICarfDataEditingChildWindow.GetFormInstanceForDataObject
        Return Nothing
    End Function
    Public Sub OrderRefreshRelatedDataControls(Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.OrderRefreshRelatedDataControls

        Dim main As ICarfParentWindow = Me.MdiParent
        main.OrderRefreshRelatedDataControls(Me, _
                                             RelatedControlDataCategoriesEnum.StockItem, _
                                             DataItem, _
                                             ActionOnDataItem)

    End Sub
    Public Sub ProcessRefreshRelatedDataControls(sender As System.Windows.Forms.Form, DataTypeCategory As RelatedControlDataCategoriesEnum, Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfDataEditingChildWindow.ProcessRefreshRelatedDataControls

        Select Case DataTypeCategory
            Case Is = RelatedControlDataCategoriesEnum.StockItem, RelatedControlDataCategoriesEnum.StockItemInventory

                AppSettings.MainForm.StatusInfoMsgPut("Data displayed in bulk stock entry were updated else where, and hence was refreshed automatically")
                Refresh_ControlsWithDataObject()

        End Select

    End Sub
    Public Sub Refresh_ControlsWithDataObject() Implements ICarfDataEditingChildWindow.Refresh_ControlsWithDataObject
        Refresh_SparesDataSet()
        Refresh_StockDataGrid()
        Refresh_CategoriesList()
        Refresh_SubCategoryList()
        Refresh_ModelList()
        Refresh_BrandList()
        Refresh_ColorList()
        Refresh_WeightList()
        Refresh_VolumeList()
        Refresh_ManufacturerList()
        Refresh_UnitList()
    End Sub
    Public Sub Refresh_DataObjectWithControls() Implements ICarfDataEditingChildWindow.Refresh_DataObjectWithControls

    End Sub
    Public Sub InitializeThisFormAndControlSettings(Optional OrderUpdateToFile As Boolean = False) Implements ICarfDataEditingChildWindow.InitializeThisFormAndControlSettings
        'init general settings
        AppSettings.InitFormSettings(Me, OrderUpdateToFile)
    End Sub
#End Region

#Region "Private Procedures"
    Private Sub Setup_StockGrid()

        If SparesStock_DataGridView.Columns.Count < 1 Then
            SparesStock_DataGridView.AutoGenerateColumns = False
            SparesStock_DataGridView.Columns.Clear()

            'SpareStockId
            Dim StockItemId_Column As New DataGridViewTextBoxColumn
            With StockItemId_Column
                .DataPropertyName = "StockItemId"
                .Name = "StockItemId_Column"
                .HeaderText = "StockItemId"
                .Visible = False
            End With
            SparesStock_DataGridView.Columns.Add(StockItemId_Column)

            'ItemCode
            Dim ItemCode_Column As New DataGridViewTextBoxColumn
            With ItemCode_Column
                .DataPropertyName = "ItemCode"
                .Name = "ItemCode_Column"
                .HeaderText = "ItemCode"
            End With
            SparesStock_DataGridView.Columns.Add(ItemCode_Column)

            'Description
            Dim Description_Column As New DataGridViewTextBoxColumn
            With Description_Column
                .DataPropertyName = "Description"
                .Name = "Description_Column"
                .HeaderText = "Description"
            End With
            SparesStock_DataGridView.Columns.Add(Description_Column)

            'Category
            Category_Column = New DatagridViewEdComboBoxColumn
            With Category_Column
                .DataPropertyName = "Category"
                .Name = "Category_Column"
                .HeaderText = "Category"
            End With
            SparesStock_DataGridView.Columns.Add(Category_Column)

            'SubCategory
            SubCategory_Column = New DatagridViewEdComboBoxColumn
            With SubCategory_Column
                .DataPropertyName = "SubCategory"
                .Name = "SubCategory_Column"
                .HeaderText = "SubCategory"
            End With
            SparesStock_DataGridView.Columns.Add(SubCategory_Column)

            'Model
            Model_Column = New DatagridViewEdComboBoxColumn
            With Model_Column
                .DataPropertyName = "Model"
                .Name = "Model_Column"
                .HeaderText = "Model"
                .Visible = False
            End With
            SparesStock_DataGridView.Columns.Add(Model_Column)

            'Brand
            Brand_Column = New DatagridViewEdComboBoxColumn
            With Brand_Column
                .DataPropertyName = "Brand"
                .Name = "Brand_Column"
                .HeaderText = "Brand"
                .Visible = False
            End With
            SparesStock_DataGridView.Columns.Add(Brand_Column)

            'Color
            Color_Column = New DatagridViewEdComboBoxColumn
            With Color_Column
                .DataPropertyName = "Color"
                .Name = "Color_Column"
                .HeaderText = "Color"
                .Visible = False
            End With
            SparesStock_DataGridView.Columns.Add(Color_Column)

            'Weight
            Weight_Column = New DatagridViewEdComboBoxColumn
            With Weight_Column
                .DataPropertyName = "Weight"
                .Name = "Weight_Column"
                .HeaderText = "Weight"
                .Visible = False
            End With
            SparesStock_DataGridView.Columns.Add(Weight_Column)

            'Volume
            Volume_Column = New DatagridViewEdComboBoxColumn
            With Volume_Column
                .DataPropertyName = "Volume"
                .Name = "Volume_Column"
                .HeaderText = "Volume"
                .Visible = False
            End With
            SparesStock_DataGridView.Columns.Add(Volume_Column)

            'Manufacturer
            Manufacturer_Column = New DatagridViewEdComboBoxColumn
            With Manufacturer_Column
                .DataPropertyName = "Manufacturer"
                .Name = "Manufacturer_Column"
                .HeaderText = "Manufacturer"
                .Visible = False
            End With
            SparesStock_DataGridView.Columns.Add(Manufacturer_Column)

            'Unit
            Unit_Column = New DatagridViewEdComboBoxColumn
            With Unit_Column
                .DataPropertyName = "Unit"
                .Name = "Unit_Column"
                .HeaderText = "Unit"
                .Visible = False
            End With
            SparesStock_DataGridView.Columns.Add(Unit_Column)

            'Quantity
            Dim Quantity_Column As New DataGridViewTextBoxColumn
            With Quantity_Column
                .DataPropertyName = "Quantity"
                .Name = "Quantity_Column"
                .HeaderText = "Quantity"
                .DefaultCellStyle.Format = "N2"
                .DefaultCellStyle.NullValue = 0
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With
            SparesStock_DataGridView.Columns.Add(Quantity_Column)

            'CostPrice
            Dim CostPrice_Column As New DataGridViewTextBoxColumn
            With CostPrice_Column
                .DataPropertyName = "CostPrice"
                .Name = "CostPrice_Column"
                .HeaderText = "CostPrice"
                .DefaultCellStyle.Format = "N2"
                .DefaultCellStyle.NullValue = 0
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With
            SparesStock_DataGridView.Columns.Add(CostPrice_Column)

            'InstallationCharge
            Dim InstallationCharge_Column As New DataGridViewTextBoxColumn
            With InstallationCharge_Column
                .DataPropertyName = "InstallationCharge"
                .Name = "InstallationCharge_Column"
                .HeaderText = "InstallationCharge"
                .DefaultCellStyle.Format = "N2"
                .DefaultCellStyle.NullValue = 0
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With
            SparesStock_DataGridView.Columns.Add(InstallationCharge_Column)

            'SellingPrice
            Dim SellingPrice_Column As New DataGridViewTextBoxColumn
            With SellingPrice_Column
                .DataPropertyName = "SellingPrice"
                .Name = "SellingPrice_Column"
                .HeaderText = "SellingPrice"
                .DefaultCellStyle.Format = "N2"
                .DefaultCellStyle.NullValue = 0
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With
            SparesStock_DataGridView.Columns.Add(SellingPrice_Column)

            'TaxPercentage
            Dim TaxPercentage_Column As New DataGridViewTextBoxColumn
            With TaxPercentage_Column
                .DataPropertyName = "TaxPercentage"
                .Name = "TaxPercentage_Column"
                .HeaderText = "TaxPercentage"
                .DefaultCellStyle.Format = "N2"
                .DefaultCellStyle.NullValue = 0
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With
            SparesStock_DataGridView.Columns.Add(TaxPercentage_Column)

            ''HasSerial
            'Dim HasSerial_Column As New DataGridViewCheckBoxColumn
            'With HasSerial_Column
            '    .DataPropertyName = "HasSerial"
            '    .Name = "HasSerial_Column"
            '    .HeaderText = "HasSerial"
            '    .TrueValue = "Y"
            '    .FalseValue = "N"
            '    .ReadOnly = True
            '    .DefaultCellStyle.NullValue = "N"
            'End With
            'SparesStock_DataGridView.Columns.Add(HasSerial_Column)

            'CreatedDate
            Dim CreatedDate_Column As New DataGridViewTextBoxColumn
            With CreatedDate_Column
                .DataPropertyName = "CreatedDate"
                .Name = "CreatedDate_Column"
                .HeaderText = "CreatedDate"
                .ReadOnly = True
                .DefaultCellStyle.Format = "dd-MMM-yy HH:mm"
                .DefaultCellStyle.NullValue = Now
                .Visible = False
            End With
            SparesStock_DataGridView.Columns.Add(CreatedDate_Column)

            'ModifiedDate
            Dim ModifiedDate_Column As New DataGridViewTextBoxColumn
            With ModifiedDate_Column
                .DataPropertyName = "LMDate"
                .Name = "ModifiedDate_Column"
                .HeaderText = "ModifiedDate"
                .ReadOnly = True
                .DefaultCellStyle.Format = "dd-MMM-yy HH:mm"
                .DefaultCellStyle.NullValue = Now
                .Visible = False
            End With
            SparesStock_DataGridView.Columns.Add(ModifiedDate_Column)
        End If

    End Sub
    Private Sub Refresh_SparesDataSet()

        Dim stkman As New StkManager(AppSettings.LoggedUser)
        Me.iDataObject = stkman.Get_StockItem_Searched(Search_TextBox.Text)
        Me.iDataObject.AcceptChanges()
        'Category
        Me.iDataCategory = stkman.Get_StockFieldHistory_Distinct("Category")
        'SubCategory
        Me.iDataSubCategory = stkman.Get_StockFieldHistory_Distinct("SubCategory")
        'Model
        Me.iDataModel = stkman.Get_StockFieldHistory_Distinct("Model")
        'Brand
        Me.iDataBrand = stkman.Get_StockFieldHistory_Distinct("Brand")
        'Color
        Me.iDataColor = stkman.Get_StockFieldHistory_Distinct("Color")
        'Weight
        Me.iDataWeight = stkman.Get_StockFieldHistory_Distinct("Weight")
        'Volume
        Me.iDataVolume = stkman.Get_StockFieldHistory_Distinct("Volume")
        'Manufacturer
        Me.iDataManufacturer = stkman.Get_StockFieldHistory_Distinct("Manufacturer")
        'Unit
        Me.iDataUnit = stkman.Get_StockFieldHistory_Distinct("Unit")

    End Sub
    Private Sub Refresh_StockDataGrid()

        SparesStock_DataGridView.DataSource = Me.iDataObject
        SparesStock_DataGridView.Refresh()

        'do existing data validation
        For Each row As DataGridViewRow In SparesStock_DataGridView.Rows
            Me.ValidateAndHighlightDataGridRow(row)
        Next

    End Sub
    Private Sub Refresh_CategoriesList()
        Category_Column.Items.Clear()
        If Me.iDataObject IsNot Nothing Then
            Dim lst As IEnumerable(Of Object) = (From sRow As DataRow In Me.iDataCategory.Rows _
                                                    Select sRow.Item("Category") _
                                                    Order By 1).Distinct
            If (lst IsNot Nothing) AndAlso (lst.Count > 0) Then
                For i As Integer = 0 To lst.Count - 1
                    Dim s As String = lst(i).ToString
                    If s <> "" Then Category_Column.Items.Add(s)
                Next
            End If
        End If
    End Sub
    Private Sub Refresh_SubCategoryList()
        SubCategory_Column.Items.Clear()
        If Me.iDataObject IsNot Nothing Then
            Dim lst As IEnumerable(Of Object) = (From sRow As DataRow In Me.iDataSubCategory.Rows _
                                                    Select sRow.Item("SubCategory") _
                                                    Order By 1).Distinct
            If (lst IsNot Nothing) AndAlso (lst.Count > 0) Then
                For i As Integer = 0 To lst.Count - 1
                    Dim s As String = lst(i)
                    If s <> "" Then SubCategory_Column.Items.Add(s)
                Next
            End If
        End If
    End Sub
    Private Sub Refresh_ModelList()
        Model_Column.Items.Clear()
        If Me.iDataObject IsNot Nothing Then
            Dim lst As IEnumerable(Of Object) = (From sRow As DataRow In Me.iDataModel.Rows _
                                                    Select sRow.Item("Model") _
                                                    Order By 1).Distinct
            If (lst IsNot Nothing) AndAlso (lst.Count > 0) Then
                For i As Integer = 0 To lst.Count - 1
                    Dim s As String = lst(i)
                    If s <> "" Then Model_Column.Items.Add(s)
                Next
            End If
        End If
    End Sub
    Private Sub Refresh_BrandList()
        Brand_Column.Items.Clear()
        If Me.iDataObject IsNot Nothing Then
            Dim lst As IEnumerable(Of Object) = (From sRow As DataRow In Me.iDataBrand.Rows _
                                                    Select sRow.Item("Brand") _
                                                    Order By 1).Distinct
            If (lst IsNot Nothing) AndAlso (lst.Count > 0) Then
                For i As Integer = 0 To lst.Count - 1
                    Dim s As String = lst(i)
                    If s <> "" Then Brand_Column.Items.Add(s)
                Next
            End If
        End If
    End Sub
    Private Sub Refresh_ColorList()
        Color_Column.Items.Clear()
        If Me.iDataObject IsNot Nothing Then
            Dim lst As IEnumerable(Of Object) = (From sRow As DataRow In Me.iDataColor.Rows _
                                                    Select sRow.Item("Color") _
                                                    Order By 1).Distinct
            If (lst IsNot Nothing) AndAlso (lst.Count > 0) Then
                For i As Integer = 0 To lst.Count - 1
                    Dim s As String = lst(i)
                    If s <> "" Then Color_Column.Items.Add(s)
                Next
            End If
        End If
    End Sub
    Private Sub Refresh_WeightList()
        Weight_Column.Items.Clear()
        If Me.iDataObject IsNot Nothing Then
            Dim lst As IEnumerable(Of Object) = (From sRow As DataRow In Me.iDataWeight.Rows _
                                                    Select sRow.Item("Weight") _
                                                    Order By 1).Distinct
            If (lst IsNot Nothing) AndAlso (lst.Count > 0) Then
                For i As Integer = 0 To lst.Count - 1
                    Dim s As String = lst(i)
                    If s <> "" Then Weight_Column.Items.Add(s)
                Next
            End If
        End If
    End Sub
    Private Sub Refresh_VolumeList()
        Volume_Column.Items.Clear()
        If Me.iDataObject IsNot Nothing Then
            Dim lst As IEnumerable(Of Object) = (From sRow As DataRow In Me.iDataVolume.Rows _
                                                    Select sRow.Item("Volume") _
                                                    Order By 1).Distinct
            If (lst IsNot Nothing) AndAlso (lst.Count > 0) Then
                For i As Integer = 0 To lst.Count - 1
                    Dim s As String = lst(i)
                    If s <> "" Then Volume_Column.Items.Add(s)
                Next
            End If
        End If
    End Sub
    Private Sub Refresh_ManufacturerList()
        Manufacturer_Column.Items.Clear()
        If Me.iDataObject IsNot Nothing Then
            Dim lst As IEnumerable(Of Object) = (From sRow As DataRow In Me.iDataManufacturer.Rows _
                                                    Select sRow.Item("Manufacturer") _
                                                    Order By 1).Distinct
            If (lst IsNot Nothing) AndAlso (lst.Count > 0) Then
                For i As Integer = 0 To lst.Count - 1
                    Dim s As String = lst(i)
                    If s <> "" Then Manufacturer_Column.Items.Add(s)
                Next
            End If
        End If
    End Sub
    Private Sub Refresh_UnitList()
        Unit_Column.Items.Clear()
        If Me.iDataObject IsNot Nothing Then
            Dim lst As IEnumerable(Of Object) = (From sRow As DataRow In Me.iDataUnit.Rows _
                                                    Select sRow.Item("Unit") _
                                                    Order By 1).Distinct
            If (lst IsNot Nothing) AndAlso (lst.Count > 0) Then
                For i As Integer = 0 To lst.Count - 1
                    Dim s As String = lst(i)
                    If s <> "" Then Unit_Column.Items.Add(s)
                Next
            End If
        End If
    End Sub
    Private Sub ChangeRowBackColor(row As DataGridViewRow, col As Color)
        For Each cell As DataGridViewCell In row.Cells
            cell.Style.BackColor = col
        Next
    End Sub
    Private Function DoesCellValueExistInHistoryData(CellValue As Object, FieldValues As DataTable) As Boolean
        If IsDBNull(CellValue) = False Then
            If (FieldValues IsNot Nothing) AndAlso (FieldValues.Rows.Count > 0) Then
                Dim lst As IEnumerable(Of Object) = From sRow As DataRow In FieldValues.Rows _
                                                                Where (IsDBNull(sRow.Item(0)) = False) _
                                                                AndAlso (sRow.Item(0) = CellValue) _
                                                                Select sRow.Item(0)
                If (lst IsNot Nothing) AndAlso (lst.Count > 0) Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function
    Private Sub ValidateAndHighlightDataGridRow(ByRef row As DataGridViewRow)
        If row.IsNewRow = False Then
            With row.Cells("Quantity_Column")
                If (IsDBNull(.Value) = False) AndAlso .Value < 1 Then
                    .ToolTipText = "Quantity is currently 0 you might want to review and revise it"
                    .Style.BackColor = Color.Yellow
                Else
                    .ToolTipText = ""
                    .Style.BackColor = SparesStock_DataGridView.DefaultCellStyle.BackColor
                End If
            End With
            With row.Cells("CostPrice_Column")
                If (IsDBNull(.Value) = False) AndAlso (.Value < 1) Then
                    .ToolTipText = "CostPrice is currently 0, which is not an acceptable value"
                    .Style.BackColor = Color.Orange
                Else
                    .ToolTipText = ""
                    .Style.BackColor = SparesStock_DataGridView.DefaultCellStyle.BackColor
                End If
            End With
            With row.Cells("InstallationCharge_Column")
                If (IsDBNull(.Value) = False) AndAlso (.Value < 1) Then
                    .ToolTipText = "InstallationCharge is currently 0, which is not an acceptable value"
                    .Style.BackColor = Color.Orange
                Else
                    .ToolTipText = ""
                    .Style.BackColor = SparesStock_DataGridView.DefaultCellStyle.BackColor
                End If
            End With
            With row.Cells("SellingPrice_Column")
                If (IsDBNull(.Value) = False) AndAlso (.Value < 1) Then
                    .ToolTipText = "SellingPrice is currently 0, which is not an acceptable value"
                    .Style.BackColor = Color.Orange
                Else
                    .ToolTipText = ""
                    .Style.BackColor = SparesStock_DataGridView.DefaultCellStyle.BackColor
                End If
            End With
            With row.Cells("TaxPercentage_Column")
                If (IsDBNull(.Value) = False) AndAlso (.Value < 1) Then
                    .ToolTipText = "TaxPercentage is currently 0, this is not common and hence be sure"
                    .Style.BackColor = Color.Yellow
                Else
                    .ToolTipText = ""
                    .Style.BackColor = SparesStock_DataGridView.DefaultCellStyle.BackColor
                End If
            End With
        End If
    End Sub
    Private Sub UpdateCheckState_FromGridToMenu()
        'ItemCode cannot be swicthed off
        'Description
        Description_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("Description_Column").Visible
        'Category
        Category_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("Category_Column").Visible
        'SubCategory
        SubCategory_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("SubCategory_Column").Visible
        'Model
        Model_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("Model_Column").Visible
        'Brand
        Brand_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("Brand_Column").Visible
        'Color
        Color_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("Color_Column").Visible
        'Weight
        Weight_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("Weight_Column").Visible
        'Volume
        Volume_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("Volume_Column").Visible
        'Manufacturer
        Manufacturer_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("Manufacturer_Column").Visible
        'Unit
        Unit_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("Unit_Column").Visible
        'Quantity
        Quantity_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("Quantity_Column").Visible
        'CostPrice
        CostPrice_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("CostPrice_Column").Visible
        'InstallationCharge
        InstallationCharge_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("InstallationCharge_Column").Visible
        'SellingPrice
        SellingPrice_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("SellingPrice_Column").Visible
        'TaxPercentage
        TaxPercentage_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("TaxPercentage_Column").Visible
        'HasSerial
        'hasSerial_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("HasSerial_Column").Visible
        'CreatedDate
        CreatedDate_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("CreatedDate_Column").Visible
        'ModifiedDate
        ModifiedDate_ToolStripMenuItem.Checked = Me.SparesStock_DataGridView.Columns("ModifiedDate_Column").Visible

        MenuCheckStateFromGridHasBeenInitialized = True
    End Sub
    Private Sub UpdateCheckState_FromMenuToGrid()
        If MenuCheckStateFromGridHasBeenInitialized Then
            Me.SparesStock_DataGridView.Columns("Description_Column").Visible = Description_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("Category_Column").Visible = Category_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("SubCategory_Column").Visible = SubCategory_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("Model_Column").Visible = Model_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("Brand_Column").Visible = Brand_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("Color_Column").Visible = Color_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("Weight_Column").Visible = Weight_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("Volume_Column").Visible = Volume_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("Manufacturer_Column").Visible = Manufacturer_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("Unit_Column").Visible = Unit_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("Quantity_Column").Visible = Quantity_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("CostPrice_Column").Visible = CostPrice_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("InstallationCharge_Column").Visible = InstallationCharge_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("SellingPrice_Column").Visible = SellingPrice_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("TaxPercentage_Column").Visible = TaxPercentage_ToolStripMenuItem.Checked
            'Me.SparesStock_DataGridView.Columns("HasSerial_Column").Visible = HasSerial_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("CreatedDate_Column").Visible = CreatedDate_ToolStripMenuItem.Checked
            Me.SparesStock_DataGridView.Columns("ModifiedDate_Column").Visible = ModifiedDate_ToolStripMenuItem.Checked

        End If
    End Sub
#End Region











    'Column list
    'ItemCode
    'Description
    'Category
    'SubCategory
    'Model
    'Brand
    'Color
    'Weight
    'Volume
    'Manufacturer
    'Unit
    'Quantity
    'CostPrice
    'SellingPrice
    'TaxPercentage
    'HasSerial
    'CreatedDate













End Class